#include "../h/iLogger.h"
#include "../h/iSingleton.h"

iLogger::iLogger()
{
    memset(m_modules,0,sizeof(m_modules));
    memset(m_levels,0,sizeof(m_levels));
}
bool iLogger::register_module(uint8_t lm, const char* mname, iLogStream* stm)
{
    if(mname == NULL)
        mname = "UNSPEC";

	if(m_modules[lm]==NULL)
		m_modules[lm] = new nstm();
    else
        return false;

    strncpy(m_modules[lm]->name, mname, sizeof(m_modules[lm]->name));
    m_modules[lm]->stm = stm;
    m_modules[lm]->name[sizeof(m_modules[lm]->name)-1]=0;
    return true;
}
void iLogger::register_level(LOGLevel lv, const char* lname, iLogStream* stm)
{
	if(NULL == lname)
        lname = "UNDEF";

	if(m_levels[lv]==NULL)
		m_levels[lv] = new nstm();
    strncpy(m_levels[lv]->name, lname, sizeof(m_levels[lv]->name));
    m_levels[lv]->stm = stm;
    m_levels[lv]->name[sizeof(m_levels[lv]->name)-1]=0;
}
void iLogger::open(const char* dir, const char* name)
{
    m_log_stream.open(dir, name);
}
void iLogger::log(uint8_t mid, uint8_t lid, uint32_t pid, uint32_t tid, const char* content)
{
	iAutoLock ial(m_lock);
    if(m_modules[mid]==NULL || m_levels[lid]==NULL || content==NULL)
        return;

    m_log_stream.write(m_modules[mid]->name, m_levels[lid]->name, pid, tid, content);
    if(m_modules[mid]->stm)
        m_modules[mid]->stm->write(m_modules[mid]->name, m_levels[lid]->name, pid, tid, content);
    if(m_levels[lid]->stm)
        m_levels[lid]->stm->write(m_modules[mid]->name, m_levels[lid]->name, pid, tid, content);
}
